<?php

namespace Platform\Models\User;

use Platform\Models\BaseModel;

class OpenUnion extends BaseModel {

    const TABLE_NAME = 'open_union';
    const type_minibeg = 1;
    const type_minirpg = 2;
    const type_wechat_servicer = 3;

    public function initialize() {
        parent::initialize();
        $this->setSource(self::TABLE_NAME);
    }

    private $id;
    private $unionId;
    private $openId;
    private $type;
    private $createTime;

    public function getId() {
        return $this->id;
    }

    public function getUnionId() {
        return $this->unionId;
    }

    public function getOpenId() {
        return $this->openId;
    }

    public function getType() {
        return $this->type;
    }

    public function getCreateTime() {
        return $this->createTime;
    }

    public function setId($id) {
        $this->id = $id;
    }

    public function setUnionId($unionId) {
        $this->unionId = $unionId;
    }

    public function setOpenId($openId) {
        $this->openId = $openId;
    }

    public function setType($type) {
        $this->type = $type;
    }

    public function setCreateTime($createTime) {
        $this->createTime = $createTime;
    }

    public function columnMap() {
        //列名 =》 属性名
        return [
            'id' => 'id',
            'union_id' => 'unionId',
            'open_id' => 'openId',
            'type' => 'type',
            'create_time' => 'createTime',
        ];
    }

    /**
     * 根据OpenId获取用户资料
     * @param type $phone
     */
    public function getDataByOpenId($openId, $type, $columns = '', $returnObject = false) {
        $query = [
            "conditions" => "openId = :openId: and type = :type:",
            "bind" => [
                "openId" => $openId,
                "type" => $type
            ],
        ];
        if ($columns) {
            $query['columns'] = $columns;
        }
        $data = self::findFirst($query);
        if ($data && $returnObject == false) {
            $data = $data->toArray();
        }
        return $data;
    }

}
